﻿<?php
$begintime = microtime(true);
require_once './lib.php';
header("Content-type:text/html;charset=utf-8");

/**
 * @param $table 需要更新或是查询的表
 * @param $qishu 云端获取的开奖期数
 * @param $code  云端获取的开奖号码
 * @param int $parnum 根据参数选择对应的方法
 */
function insLotteryResult($table, $qishu, $code, $parnum = 5)
{
    $mysql = new DBAccess();
    if ($parnum == 5) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getValue($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
                "ball_4" => $ball[3],
                "ball_5" => $ball[4],
            );
            $mysql->insertRow($table, $ins_res);
        }
    } elseif ($parnum == 3) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getValue($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
            );
            $mysql->insertRow($table, $ins_res);
        }
    } elseif ($parnum == 8) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getValue($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
                "ball_4" => $ball[3],
                "ball_5" => $ball[4],
                "ball_6" => $ball[5],
                "ball_7" => $ball[6],
                "ball_8" => $ball[7],
            );
            $mysql->insertRow($table, $ins_res);
        }
    } elseif ($parnum == 10) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getValue($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
                "ball_4" => $ball[3],
                "ball_5" => $ball[4],
                "ball_6" => $ball[5],
                "ball_7" => $ball[6],
                "ball_8" => $ball[7],
                "ball_9" => $ball[8],
                "ball_10" => $ball[9],
            );
            $mysql->insertRow($table, $ins_res);
        }
    } elseif ($parnum == 20) {
        $sql = "select id from $table  WHERE qishu='$qishu'";
        $ball = explode(',', $code);
        if (!$mysql->getValue($sql)) {
            $ins_res = array(
                'qishu' => $qishu,
                'create_time' => date('Y-m-d H:i:s'),
                'datetime' => date('Y-m-d H:i:s'),
                "ball_1" => $ball[0],
                "ball_2" => $ball[1],
                "ball_3" => $ball[2],
                "ball_4" => $ball[3],
                "ball_5" => $ball[4],
                "ball_6" => $ball[5],
                "ball_7" => $ball[6],
                "ball_8" => $ball[7],
                "ball_9" => $ball[8],
                "ball_10" => $ball[9],
                "ball_11" => $ball[10],
                "ball_12" => $ball[11],
                "ball_13" => $ball[12],
                "ball_14" => $ball[13],
                "ball_15" => $ball[14],
                "ball_16" => $ball[15],
                "ball_17" => $ball[16],
                "ball_18" => $ball[17],
                "ball_19" => $ball[18],
                "ball_20" => $ball[19],
            );
            $mysql->insertRow($table, $ins_res);
        }
    }
}

$typeId = array(
    array('type' => 6, 'code' => '/ElevenFive/getElevenFiveInfo.do?lotCode=10006'),                 //广东11选5
    array('type' => 1, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10002'),                     //重庆时时彩
    array('type' => 18, 'code' => '/pks/getLotteryPksInfo.do?lotCode=10001'),                        //北京PK拾
    array('type' => 10, 'code' => '/QuanGuoCai/getLotteryInfo1.do?&lotCode=10043'),                  //排列3
    array('type' => 9, 'code' => '/QuanGuoCai/getLotteryInfo1.do?&lotCode=10041'),                  //福彩3D
    array('type' => 36, 'code' => '/CQShiCai/getBaseCQShiCai.do?lotCode=10003'),                     //天津时时彩
    array('type' => 38, 'code' => '/gxklsf/getKlsfDoubleCount.do?lotCode=10038'),                    //广西十分彩
    array('type' => 5, 'code' => '/klsf/getKlsfDoubleCount.do?lotCode=10005'),                      //广东快乐十分
    array('type' => 34, 'code' => '/klsf/getKlsfDoubleCount.do?lotCode=10034'),                      //天津快乐十分
    array('type' => 7, 'code' => '/lotteryJSFastThree/getBaseJSFastThree.do?issue=&lotCode=10007'), //上海时时乐（修改江苏快3）
    array('type' => 14, 'code' => '/LuckTwenty/getBaseLuckTewnty.do?issue=&lotCode=10014')          //北京快乐8
);

//从Cookie中取出历史期数数组 unserialize 对单一的已经序列化的变量进行操作，将其转换回PHP的值；
$arr = isset($_COOKIE['qishu']) ? unserialize($_COOKIE['qishu']) : array();
/**
 * 循环将数据保存到数据库对应表中
 */
foreach ($typeId as $k => $v) {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'api.api68.com' . $v['code']);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    $data = curl_exec($curl);
    $data = json_decode(trim($data), true);
    curl_close($curl);
    $data = $data['result']['data'];
    $c_t = trim($data['preDrawIssue']); //开奖期数
    $c_r = trim($data['preDrawCode']); //开奖号码
    $type = $v['type'];//开奖类型
    $qstype = $type."-".$c_t;;//将数据存到cookie中，因为存在可能期数为相同 则根据彩种类型编号与期号拼接存到cookie中

    //把最新的浏览历史放于数组末尾位置
    if (!in_array($qstype, $arr)) {
        if ($type == 1) {

            insLotteryResult("lottery_result_cq", $c_t, $c_r, 5);

        } elseif ($type == 18) {

            insLotteryResult("lottery_result_bjpk", $c_t, $c_r, 10);

        } elseif ($type == 6) {

            insLotteryResult("lottery_result_gd11", $c_t, $c_r, 5);

        } elseif ($type == 16) {

            insLotteryResult("lottery_result_jx", $c_t, $c_r, 5);

        } elseif ($type == 10) {

            insLotteryResult("lottery_result_p3", $c_t, $c_r, 3);

        } elseif ($type == 9) {

            insLotteryResult("lottery_result_d3", $c_t, $c_r, 3);

        } elseif ($type == 36) {

            insLotteryResult("lottery_result_tj", $c_t, $c_r, 5);

        } elseif ($type == 38) {

            insLotteryResult("lottery_result_gxsf", $c_t, $c_r, 5);

        } elseif ($type == 5) {

            insLotteryResult("lottery_result_gdsf", $c_t, $c_r, 8);

        } elseif ($type == 34) {

            insLotteryResult("lottery_result_tjsf", $c_t, $c_r, 8);

        } elseif ($type == 7) {

            insLotteryResult("lottery_result_t3", $c_t, $c_r, 3);

        } elseif ($type == 14) {

            insLotteryResult("lottery_result_bjkn", $c_t, $c_r, 20);

        }
       //将最新的数据存入cookie
        array_unshift($arr, $qstype);
        //去掉重复值
        $arr = array_unique($arr);
        $arr = array_slice($arr, 0, 15);

    }
}
setcookie('qishu', serialize($arr), time()+86400, '/');
$endtime = microtime(true);
$resultime = $endtime - $begintime;
echo "页面运行时间：$resultime";
